﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

/// <summary>
/// Summary description for General
/// </summary>
namespace Datalayer
{
    public class General
    {
        public General()
        {

        }
        public bool IsValidImagesFile(FileInfo file)
        {
            string strImageExts = ";.gif;.jpg;.jpeg;.bmp;.png;";
            string ext = file.Extension.ToLower();
            if (strImageExts.IndexOf(ext) >= 0) return true;
            return false;
        }
        public bool IsValidImagesFile(HttpPostedFile file)
        {
            string strImageExts = ";.gif;.jpg;.jpeg;.bmp;.png;";
            string ext = Path.GetExtension(file.FileName).ToLower();
            if (strImageExts.IndexOf(ext) >= 0) return true;
            return false;
        }

        public bool checkFilesize(FileInfo file)
        {
            if (file.Length > 1024 * 512) //512 KB
            {
                return false;
            }
            return true;
        }
        public bool checkFilesize(HttpPostedFile file)
        {
            if (file.ContentLength > 1024 * 512) //512 KB
            {
                return false;
            }
            return true;
        }

        public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFile);
            // Prevent using images internal thumbnail
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
            FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

            if (OnlyResizeIfWider)
            {
                if (FullsizeImage.Width <= NewWidth)
                {
                    NewWidth = FullsizeImage.Width;
                }
            }

            int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width;
            if (NewHeight > MaxHeight)
            {
                // Resize with height instead
                NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;
                NewHeight = MaxHeight;
            }
            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

            // Clear handle to original file so that we can overwrite it if necessary
            FullsizeImage.Dispose();

            // Save resized picture
            if (NewFile == null || NewFile == "")
            {
                File.Delete(OriginalFile);
                NewImage.Save(OriginalFile);
            }
            else
                NewImage.Save(NewFile);

        }

        public string Upload(FileInfo file, string fname, string ServerFolder)
        {
            try
            {
                DateTime dt = DateTime.Now;
                string seconds = dt.Day + "" + dt.Month + "" + dt.Year + "" + dt.Hour + "" + dt.Second + "" + dt.Millisecond + "" + dt.Ticks;

                string fileName = "";
                string fileType = "";

                if (fname == null || fname == "")
                {
                    fileName = file.Name.Replace("_", "");
                    fileName = fileName.Substring(0, fileName.LastIndexOf('.'));
                    fileType = file.Extension;
                }
                else
                {
                    fileName = fname.Replace("_", "");
                    fileName = fileName.Substring(0, fileName.LastIndexOf('.'));
                    fileType = fname.Substring(fname.LastIndexOf('.'));
                }

                fileName = "f" + fileName.GetHashCode().ToString() + seconds + fileType;
                fileName = fileName.Replace("-", "");

                string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + ServerFolder, fileName);

                File.Copy(file.FullName, savedFileName, true);
                return fileName;
            }
            catch { }
            return "";
        }
        public string Upload(HttpPostedFile file, string fname, string ServerFolder)
        {
            try
            {
                DateTime dt = DateTime.Now;
                string seconds = dt.Day + "" + dt.Month + "" + dt.Year + "" + dt.Hour + "" + dt.Second + "" + dt.Millisecond + "" + dt.Ticks;

                string fileName = "";
                string fileType = "";

                if (fname == null || fname == "")
                {
                    fileName = file.FileName.Replace("_", "");
                    fileName = fileName.Substring(0, fileName.LastIndexOf('.'));
                    fileType = Path.GetExtension(file.FileName);
                }
                else
                {
                    fileName = fname.Replace("_", "");
                    fileName = fileName.Substring(0, fileName.LastIndexOf('.'));
                    fileType = fname.Substring(fname.LastIndexOf('.'));
                }

                fileName = "f" + fileName.GetHashCode().ToString() + seconds + fileType;
                fileName = fileName.Replace("-", "");

                string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + ServerFolder, fileName);

                file.SaveAs(savedFileName);
                return fileName;
            }
            catch { }
            return "";
        }

        public object ConvertToDate(string date)
        {
            if (date == null || date.Length != 10) return null;
            try
            {
                DateTime dte = new DateTime(int.Parse(date.Substring(6, 4)), int.Parse(date.Substring(3, 2)), int.Parse(date.Substring(0, 2)));
                return dte;
            }
            catch
            { }
            return null;
        }

        public string getShort(string src, int skt)
        {
            string s = Strip(src);
            if (s.Length > skt)
            {
                s = s.Substring(0, skt);
                s = s.Substring(0, s.LastIndexOf(" ")) + "...";
            }
            return s;
        }
        public string Strip(string text)
        {
            return System.Text.RegularExpressions.Regex.Replace(text, @"<(.|\n)*?>", String.Empty);
        }


    }
}